package com.szl.sbssh.service.impl;

import com.szl.sbssh.constants.Constants;
import com.szl.sbssh.jpa.ResouceRepository;
import com.szl.sbssh.model.Tresource;
import com.szl.sbssh.model.Trole;
import com.szl.sbssh.model.Tuser;
import com.szl.sbssh.pageModel.base.SessionInfo;
import com.szl.sbssh.pageModel.base.Tree;
import com.szl.sbssh.pageModel.sys.Resource;
import com.szl.sbssh.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author: songzhonglin
 * Date: 2018/4/18
 * Time: 9:19
 * Description:
 **/
@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private ResouceRepository resouceRepository;

    @Override
    public List<Tree> tree(SessionInfo sessionInfo) {
        List<Tree> lt = new ArrayList<Tree>();
        List<Tresource> l = resouceRepository.findAll(new Specification<Tresource>() {
            @Nullable
            @Override
            public Predicate toPredicate(Root<Tresource> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                // 如果是超级管理员，则查询所有订单
                if (sessionInfo.getUsertype().equals(Constants.SUPERADMIN)) {
                    Predicate p1 = cb.equal(root.get("resourcetype").as(Integer.class), 0);
                    Predicate p2 = cb.equal(root.get("state").as(Integer.class), 0);
                    query.where(cb.and(p1, p2));
                    //添加排序的功能
                    query.orderBy(cb.asc(root.get("seq").as(Integer.class)));
                    return query.getRestriction();
                } else {
                    Join<Tresource, Trole> resourceTroleJoin = root.join("roles", JoinType.LEFT).join("users", JoinType.LEFT);
//                    Join<Trole, Tuser> roleTuserJoin = root.join("users", JoinType.LEFT);
                    Predicate p1 = cb.equal(root.get("resourcetype").as(Integer.class), 0);
                    Predicate p2 = cb.equal(resourceTroleJoin.get("id").as(Long.class), sessionInfo.getId());
                    Predicate p3 = cb.equal(root.get("state").as(Integer.class), 0);
                    query.where(p1, p2,p3);
                    // 添加排序的功能
                    query.orderBy(cb.asc(root.get("seq").as(Integer.class)));
                    return query.getRestriction();
                }
            }
        });
        if ((l != null) && (l.size() > 0)) {
            for (Tresource r : l) {
                Tree tree = new Tree();
                tree.setId(r.getId().toString());
                if (r.getResource() != null) {
                    tree.setPid(r.getResource().getId().toString());
                } else {
                    tree.setState("closed");
                }
                tree.setText(r.getName());
                tree.setIconCls(r.getIcon());
                Map<String, Object> attr = new HashMap<String, Object>();
                attr.put("url", r.getUrl());
                tree.setAttributes(attr);
                lt.add(tree);
            }
        }

        return lt;
    }

    @Override
    public List<Resource> treeGrid() {
        return null;
    }

    @Override
    public void add(Resource resource) {

    }

    @Override
    public void delete(Long id) {

    }

    @Override
    public void edit(Resource resource) {

    }

    @Override
    public Resource get(Long id) {
        return null;
    }

    @Override
    public List<Tree> listAllTree(boolean flag) {
        return null;
    }

    @Override
    public List<String> listAllResource() {
        List<String> resourceList = new ArrayList<String>();

        List<Tresource> l = resouceRepository.findAll(new Specification<Tresource>() {
            @Nullable
            @Override
            public Predicate toPredicate(Root<Tresource> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Join<Tresource, Tresource> tresourceTresourceJoin = root.join("resource", JoinType.LEFT);
                // 添加排序的功能
                query.orderBy(cb.asc(root.get("seq").as(Integer.class)));
                return query.getRestriction();
            }
        });
        for (int i = 0; i < l.size(); i++) {
            resourceList.add(l.get(i).getUrl());
        }
        return resourceList;
    }
}
